/* 526. 优美的排列 */
/**
 * @param {number} n
 * @return {number}
 */
var countArrangement = function (n) {
	const vis = new Array(n + 1).fill(0)
	const match = new Array(n + 1).fill(0).map(() => [])
	let num = 0

	for (let i = 1; i <= n; i++) {
		for (let j = 1; j <= n; j++) {
			if (i % j === 0 || j % i === 0) {
				match[i].push(j)
			}
		}
	}
	const backtrack = (index, n) => {
		if (index === n + 1) {
			num++
			return
		}
		for (const x of match[index]) {
			if (!vis[x]) {
				vis[x] = true
				backtrack(index + 1, n)
				vis[x] = false
			}
		}
	}

	backtrack(1, n)
	return num
}

countArrangement(2)
